import {Component, OnInit, Input} from '@angular/core';
import {ApiService} from "@core/services/http/api.service";
import {GridConfigService} from 'app/core/config/grid-config.service';
import {GridOptions} from '@ag-grid-community/core';
import {ActivatedRoute}  from "@angular/router";
import {ApesMessageService} from "@shared/apes/message";
import {UserAuthService} from "@core/services/user-auth.service";
import { BiService } from '../../bi.service';

@Component({
  selector: 'apes-control-pool',
  templateUrl: './control-pool.component.html',
  styles: []
})
export class ControlPoolComponent implements OnInit {
  buttonBar = [{hide: true, style: 'default', label: '查询', id: 'action_query',loading: false},
  ];
  title = '';
  ctx = {model: {}};
  organizationData : any[];
  rbCount:any[] = [];
  rsCount:any[] = [];
  qtCount:any[] = [];
  /** Grid 相关 **/
  resultData: any;
  gridOptions: GridOptions;
  pinnedBottomRowData: any[] = [];
  columnDefs: any[] = [];
  height: any = 400 + 'px';
  userInfo = this.userAuthService.getUserInfoObj();
  companyId = this.userInfo.userAccount.post.company.id;
  companyData: any;
  companyList: any[];

  constructor(private apiService: ApiService,
              private gridConfigService: GridConfigService,
              private routerInfo: ActivatedRoute,
              private msg: ApesMessageService,
              private userAuthService: UserAuthService,
              private bi: BiService) {
    this.columnDefs = [
      {headerName: '案件数量',
        children: [
          { headerName: "跟踪部门",field: 'GZBM'  }
        ]
      },
      {headerName: '会员报案',
        children: [
          { headerName: "今天" ,field: 'FRIST' },
          { headerName: "2~3天" ,field: 'SECONED' },
          { headerName: "4~7天" ,field: 'THREE' },
          { headerName: "8~15天" ,field: 'FOUR' },
          { headerName: "16~30天",field: 'FIVE'  },
          { headerName: "30天以上",field: 'SIX'  }
        ]
      },
      {headerName: '合作推送（合并门店接案+商务推送+合作推送）',
        children: [
          { headerName: "今天" ,field: 'FRIST_M' },
          { headerName: "2~3天" ,field: 'SECONED_M' },
          { headerName: "4~7天",field: 'THREE_M'  },
          { headerName: "8~15天",field: 'FOUR_M' },
          { headerName: "16~30天",field: 'FIVE_M'  },
          { headerName: "30天以上",field: 'SIX_M'  }
        ]
      }
    ];

    // 初始化 grid
    this.gridOptions = <GridOptions>{
      toolPanelSuppressSideButtons: true,
      suppressExcelExport: true,
      suppressCsvExport: true,
      showToolPanel: false,
      pagination: true,
      paginationPageSize: 30,
      rowHeight: 35
    };
    this.gridOptions.localeText = gridConfigService.getLocaleText();
    this.companyList = [];
    this.companyList.push(this.companyId);
  }

  ngOnInit() {
    this.bi.getCompany(this.userInfo.userAccount.post.localPermit.id).then(value => { this.companyData = value });
    this.submit();
  }

  onEvent(data: any) {
    if (data instanceof MouseEvent) return;
    if (data) {
      if (data.id === 'action_query') {
        this.buttonBar[0].loading = true;
        this.submit();
      }
    }
  }

  submit() {
    // if(this.companyList.length <= 0){
    //   this.companyList.push(this.companyId);
    // }
    this.getTablesData();
    this.buttonBar[0].loading = false;
  }

  getTablesData() {
    let data = { 'interfaceMethod': 'ipmBusinessStatementService.getAjgzjkHZ', 'pdata': { 'companyId': this.companyList } };
    this.apiService.post('report.interface', { 'body': JSON.stringify(data) }).subscribe(
      (resultData) => {
        // debugger;
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            let hearderTot = data['AJGZHZ'];
            for (let i = 0, len = hearderTot.length; i < len; i++) {
              let tempData = hearderTot[i];
              tempData.headerConten = data[tempData.id];
            }
            this.organizationData = hearderTot;
            this.rbCount = data['RB'];
            this.rsCount = data['RS'];
            this.qtCount = data['QTGS'];
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

  headerConten = [
    // { track: '会员报案' , headerTotal: ''},
    { track: '今天', headerTotal: 'FRIST', headerTitle: 'FRIST_M' },
    { track: '2~3天', headerTotal: 'SECONED', headerTitle: 'SECONED_M' },
    { track: '4~7天', headerTotal: 'THREE', headerTitle: 'THREE_M' },
    { track: '8~15天' , headerTotal: 'FOUR', headerTitle: 'FOUR_M'},
    { track: '16~30天', headerTotal: 'FIVE', headerTitle: 'FIVE_M' },
    { track: '30天以上' , headerTotal: 'SIX', headerTitle: 'SIX_M'},
    // { track: '合作推送' , headerTotal: ''},
    // { track: '今天', headerTotal: 'FRIST_M' },
    // { track: '2~3天', headerTotal: 'SECONED_M' },
    // { track: '4~7天', headerTotal: 'THREE_M' },
    // { track: '8~15天' , headerTotal: 'FOUR_M'},
    // { track: '16~30天', headerTotal: 'FIVE_M' },
    // { track: '30天以上' , headerTotal: 'SIX_M'},
  ];


}
